<?php
/**
 *
 * ClassName: Htmlsearchparser_model
 *
 * description...
 *
 * @author lizhifeng <lizhifeng@soften.cn>
 *
 */
 
class Sina_detial_model extends CI_Model{

	var $wbfield = array(
			'_id'			 =>'',//微博ID
			//'idstr'		     =>'',//字符串型的微博ID
			'text'		     =>'',//微博信息内容
			'ctime'		     =>'',//微博创建时间
			'source'		 =>'',//微博来源
			'reposts_count'  =>'',//转发数
			'comments_count' =>'',//评论数
			'attitudes_count'=>'',//表态数(好评数)
			'retweeted_id'	 =>'',//转载的微博ID
			'retweeted_uid'	 =>'',//转载的微博用户ID
			'retweeted_name' =>'',//转载的微博用户名
			'url'			 =>'',//详情URL
			'thumbnail_pic'  =>'',//缩略图片地址，没有时不返回此字段
			'bmiddle_pic'	 =>'',//中等尺寸图片地址，没有时不返回此字段
			'original_pic'   =>'',//原始图片地址，没有时不返回此字段
			'addtime'		 =>'',//增加时间
			'source_myid'	 =>'',//渠道ID抓取渠道
			'user' => Array(
					'id'				=>'',//用户ID
					'profile_url'	    =>'',//用户空间URL
					'name'	            =>'',//昵称
					'description'       =>'',//描述
					'header_url'        =>'',//头像
                    'followers_count'   =>'',//粉丝数
					'friends_count'     =>'',//关注数
					'verified'		    =>'',//是否加V
				)
		 );


	public function __construct() 
	{
		parent::__construct();
		$this->load->helper('MY_common_helper');
        $this->load->helper('MY_http_helper');
	}


    function get_sina_weibo_user_info($html) {
        $html=$this->PreHand($html);
        $Patten='#<div class="PCD_counter">.*?<strong[^>]*>(?P<friends_count>.*?)</strong>.*?<strong[^>]*>(?P<followers_count>.*?)</strong>#ims';
        if(preg_match_all($Patten,$html,$Match))
        {
            return array('friends_count' => $Match['friends_count'][0], 'followers_count' => $Match['followers_count'][0]);
        }
        else
        {
            log_message ( 'error', "cannot fetch userinfo from html", FALSE, 'hot/sina' );
            return false;
        }
    }

 	public  function get_sina_weibo_detial($html)
    {

    	$html=$this->PreHand($html);
    	$imgPatten='/<img(?:.*?)src="(?P<image>.*?)"(?:.*?)>/' ; 	
		$p1='#http://weibo.com/(.*?)/(.*)#' ;
		$par = "/[\x80-\xff]/";
        
		$Patten='#<div class="face">(.*?)usercard="id=(?P<id>.*?)&type(.*?)src="(?P<header_url>.*?)"(.*?)alt="(?P<name>.*?)"#ims';

		//$file_name = "/data/www/crawl/application/logs/sina.txt";
		//file_put_contents($file_name,$html,FILE_APPEND);
        	
    	if(preg_match_all($Patten,$html,$Match))
    	{
				$i = 0 ;
    			$user=array();	
    			$user['id'] = $Match['id'][$i];
     			$user['name']= $Match['name'][$i] ;		
    			$user['profile_url']='http://weibo.com/u/'.$user['id']; // 直接合并吧
    			$user['header_url']=$Match['header_url'][$i];

            // 这两个从本页面获取不到
            /*
    			$user['followers_count']=$Match['followers_count'][$i];
    			$user['friends_count']=$Match['friends_count'][$i];   			
            */

//            pr($Match);
    			if(!strpos($html,'W_icon icon_approve_co'))
    			{
    				$user['verified']=0 ;	
    			}
    			else 
    			{
    				$user['verified'] = 1;
    			}

            $Patten='#<div minfo=".*?mid="(?P<_id>.*?)".*?<div class="WB_from S_txt2">\s*<a[^>]*date="(?P<ctime>.*?)"[^>].*?>*?来自\s*<a[^>]*>(?P<source>[^<]*?)</a>.*?<div class="WB_text W_f14"(.*?)>(?P<text>.*?)</div>#mis';
            $Patten='~<div class="WB_from S_txt2">\s*<a[^>]*date="(?P<ctime>.*?)"[^>]*?value=feed_time:(?P<_id>.*?)">.*?来自\s*<a[^>]*>(?P<source>[^<]*?)</a>.*?<div class="WB_text W_f14".*?>(?P<text>.*?)</div>~mis';
//            $Patten='#<div minfo=".*?mid="(?P<_id>.*?)".*?<div class="WB_from S_txt2">\s*<a[^>]*date="(?P<ctime>.*?)"[^>]*>*?#';

    			if(preg_match_all($Patten,$html,$Match))
    			{			
    				$Wcontent =trim($Match['text'][$i]) ;  				
    				$Wcontent=preg_replace('/<img render="ext"(.*?)title="(.*?)"(.*?)>/','${2}',$Wcontent);
    				$Wcontent=preg_replace('/<img.*?>/','',$Wcontent);
    				$rTemp['text'] =trim($Wcontent) ;   				
    				$rTemp['_id']=trim($Match['_id'][$i]) ;
    				@$rTemp['ctime']=substr($Match['ctime'][$i],0,10);
    				$rTemp['source']=$Match['source'][$i];
    			}
    			else 
    			{
                    return  FALSE;
    			}
    			
            $Patten='#<div class="WB_feed_handle".*?<em.*?ficon_forward.*?</em><em>(?<reposts_count>.*?)</em>.*?<em.*?ficon_repeat.*?<em>(?<comments_count>.*?)</em>.*?<em.*?icon_praised.*<em>(?<attitudes_count>.*?)</em>#';
    			if(preg_match_all($Patten,$html,$Match))
    			{

    				$rTemp['reposts_count']=preg_replace($par,"",$Match['reposts_count'][$i]) ;
    				$rTemp['comments_count']=preg_replace($par,"",$Match['comments_count'][$i]);
    				$rTemp['attitudes_count']=$Match['attitudes_count'][$i] ;
    			}

    			$Patten = '/<div class="text S_txt2">(?P<description>.*?)<\/div>/' ;
    			
    			if(preg_match_all($Patten,$html,$Match))
    			{
    				$user['description'] = $Match['description'][$i];
    			}
    			
    			$rTemp['text'] = preg_replace("/(<.*?>)/","",$rTemp['text']);			
    			$rTemp['user']=$user ;
    			$rTemp['addtime'] = time();

    		return  $rTemp ;
    	}
    	else 
    	{
    		log_message ( 'error', "can not Extraction message list from hotparser_model", FALSE, 'hot/sina' );
    		return false;
    		// 如果无法匹配到微博列表  可能原因是抓取的页面不正确 或者 是网页改版所导致
    	}
    }
     
	private function PreHand($str)
	{
		$str=preg_replace('/^\s*$/m', '', $str);
		$str=preg_replace('/\n{2}/', "\n", $str);
		$str = str_replace("\r\n","",$str);
		$str = str_replace("\r","",$str);
		$str = str_replace("\n","",$str);	
		$str = str_replace("\\r\\n","",$str);
		$str = str_replace("\\r","",$str) ;
		$str = str_replace("\\n","",$str);
		$str = str_replace("\\t","",$str);
		$str=stripslashes($str);
		return trim($str);
	}
	
	function get_sina_weibo_commt($json_comm)
	{
		$comm = json_decode($json_comm,true);
		
		if($comm&&isset($comm['data']['html']))
		{
			$html = $this->PreHand($comm['data']['html']);	
		}
		else 
		{
			return false;
		}
		
		//$filename = "/data/www/crawl/application/logs/weibo.txt";
		//file_put_contents($filename, $html);
			
		$pattern = "#(<div comment_id=.*?</div>\s*?</div>\s*?</div>\s*?</div>)#";
		$datas = array();
		
		if(preg_match_all($pattern,$html,$matches))
		{
			$pattern = "#<div comment_id=\"(.*?)\".*?alt=\"(.*?)\".*?src=\"(.*?)\".*?<div class=\"list_con\" node-type=\"replywrap\">.*?usercard=\"id=(.*?)\".*?</a>(.*?)</div>.*?<div class=\"WB_from S_txt2\">(.*?)</div>#";			
			foreach($matches[1] as $match)
			{
				if(preg_match($pattern,$match,$reply_item))
				{		
					$data['id']=$reply_item[1];
					$data['screen_name']=$reply_item[2];
					$data['header_url']=$reply_item[3];
					$data['profile_url']="http://weibo.com/u/".$reply_item[4];
					$data['txt']=preg_replace("/(<.*?>)/","",$reply_item[5]);
					$data['txt'] = str_replace("：","",$data['txt']);
					$data['created_at']=$this->str2time($reply_item[6]);
					$datas[]=$data;
				}
			}
		}
		
		return $datas;
		
	}
	
	function str2time($str){
		$today = date("Y-m-d");
		if(strpos($str,"月")==true && strpos($str,'年')===false){
			$str = date("Y")."年".$str;
		}
		$str = str_replace(
			array("秒前","分钟前","小时前","今天","年","月","日"),
			array("seconds ago","minutes ago","hours ago",$today,"-","-","")
			,$str);
		return date("Y-m-d H:i:s",strtotime($str));
	}
}

?>
